package com.cmccgz.udacs_landlord_webapp.main.housemanager.function;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseFragment;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.HousePeopleAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.HousePeopleBean;
import com.cmccgz.udacs_landlord_webapp.bean.MobileBean;
import com.cmccgz.udacs_landlord_webapp.main.housemanager.list.HouseListActivity;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/6/2 16:49
 */

public class HouseExchangeFragment extends BaseFragment implements HouseFunctionContract.ExchangeHouseView, View.OnClickListener {

    public static final int CODE_REQUEST = 0x232;
    private HouseFunctionContract.ExchangeHousePresenter mPresenter;
    private TextView tv_house_name;
    private TextView tv_new_house_name;
    private RecyclerView rv_list;
    private HousePeopleAdapter adapter;
    private String houseName;
    private String houseId;
    private ViewHelper mViewHelper;
    private Handler handler;
    private LinearLayout ll_new_house;


    public static HouseExchangeFragment newInstance() {
        return new HouseExchangeFragment();
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_house_exchange;
    }

    @Override
    protected void initView() {
        handler = new Handler();
        initBundle();
        initTitle();
        findView();
        initRecyclerView();
        showLoadingDialog("正在获取租客详情，请稍侯...");
        mPresenter.getDetail(houseId);
    }

    private void initBundle() {
        Bundle bundle = getArguments();
        houseName = bundle.getString(HouseFunctionActivity.HOUSE_NAME);
        houseId = bundle.getString(HouseFunctionActivity.HOUSE_ID);
    }

    private void initRecyclerView() {
        adapter = new HousePeopleAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_list.setLayoutManager(layoutManager);
        rv_list.setAdapter(adapter);
        rv_list.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
    }


    private void findView() {
        tv_house_name = (TextView) findViewById(R.id.tv_house_name);
        tv_house_name.setText(houseName);
        rv_list = (RecyclerView) findViewById(R.id.rv_list);
        tv_new_house_name = (TextView) findViewById(R.id.tv_new_house_name);
        ll_new_house = (LinearLayout) findViewById(R.id.ll_new_house);
        ll_new_house.setOnClickListener(this);
    }

    private void initTitle() {
        mViewHelper = ViewHelper.instance(this);
        if (mViewHelper != null) {
            mViewHelper.setTitle("换房");
            mViewHelper.setAllVisable(false);
            mViewHelper.setBackTextVisable(true);
            mViewHelper.setTextRightVisable(true);
            mViewHelper.setRightText("完成");
        }
    }

    @Override
    public void setPresenter(HouseFunctionContract.ExchangeHousePresenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }

    @Override
    public void getDataSuccess(List<HousePeopleBean> list) {
        dismissLoadingDialog();
        adapter.notifyData(list);
        if (list.size() == 0){
            mViewHelper.setTextRightVisable(false);
            ll_new_house.setEnabled(false);
            showToast("当前房屋没有租客！");
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mContext!=null) {
                        mContext.finish();
                    }
                }
            },2000);
        }else {
            mViewHelper.setTextRightVisable(true);
            ll_new_house.setEnabled(true);
        }
    }

    @Override
    public void getDataFailed(String errorMsg) {
        dismissLoadingDialog();
        showToast(errorMsg);
    }

    @Override
    public void commitSuccess() {
        dismissLoadingDialog();
        showToast("换房成功！");
        mContext.setResult(Activity.RESULT_OK);
        mContext.finish();
    }

    @Override
    public void commitFailed(String errorMsg) {
        dismissLoadingDialog();
        showToast(errorMsg);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                mContext.finish();
                break;
            case R.id.ll_new_house:
                Intent intent = new Intent(mContext, HouseListActivity.class);
                intent.putExtra(HouseListActivity.TYPE, HouseListActivity.TYPE_EXCHANGE);
                intent.putExtra(HouseListActivity.IS_FOR_SELECTED, true);
                mContext.startActivityForResult(intent, CODE_REQUEST);
                break;
            case R.id.tv_right:
                List<String> selectedPeople = adapter.getSelectedPeople();
                Logger.e("selectedPeople数量：" + selectedPeople.size());
                if (selectedPeople.size() == 0) {
                    showToast("请选择租客！");
                    return;
                }
                if (tv_new_house_name.getTag() == null){
                    showToast("请选择新房屋！");
                    return;
                }
                List<MobileBean> mobileList = new ArrayList<>();
                for (String mobile : selectedPeople) {
                    mobileList.add(new MobileBean(mobile));
                }
                showLoadingDialog("正在提交数据，请稍候...");
                mPresenter.commit(mobileList, houseId,(String) tv_new_house_name.getTag());
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CODE_REQUEST && resultCode == HouseListActivity.RESULT_CODE) {
            String house_name = data.getStringExtra(HouseListActivity.HOUSE_NAME);
            String house_id = data.getStringExtra(HouseListActivity.HOUSE_ID);
            tv_new_house_name.setText(house_name);
            tv_new_house_name.setTag(house_id);
        }
    }
}
